package com.zh.webcommon.common.avatar;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Base64;

/**
 * @author ZH
 * @date 17:50 2021/12/7
 */
public class RetroAvatarCreate {
    private static final AvatarConfig avatarConfig = new AvatarConfig();

    private RetroAvatarCreate() {
    }

    public static String createAvatarBase64() {
        BufferedImage image = createAvatarImage();
        //输出流
        try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
            ImageIO.write(image, "jpg", stream);
            return Base64.getEncoder().encodeToString(stream.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static byte[] createAvatarBytes() {
        BufferedImage image = createAvatarImage();
        //输出流
        try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
            ImageIO.write(image, "png", stream);
            return stream.toByteArray();
        } catch (IOException ignored) {
            return new byte[0];
        }
    }

    public static File createAvatarFile() {
        BufferedImage image = createAvatarImage();

        //输出到临时文件
        File tempFile = null;
        try {
            tempFile = Files.createTempFile("retro-avatar", ".jpeg").toFile();
            ImageIO.write(image, "jpeg", tempFile);
            return tempFile;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (tempFile != null)
                tempFile.deleteOnExit();
        }
    }

    private static BufferedImage createAvatarImage() {
        BufferedImage avatarImage;

        int cellCount = avatarConfig.getCellCount();
        int padding = avatarConfig.getPadding();
        int cellSize = avatarConfig.getCellSize();
        int avatarLength = 2 * padding + cellCount * cellSize;
        int[][] data = DataGenerator.generateData(cellCount);

        avatarImage = new BufferedImage(avatarLength, avatarLength, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = avatarImage.createGraphics();

        if (avatarConfig.getTransparent()) {
            // transparent
            avatarImage = g.getDeviceConfiguration().createCompatibleImage(avatarLength, avatarLength, Transparency.TRANSLUCENT);
            g.dispose();
            g = avatarImage.createGraphics();
        } else {
            // if not transparent, draw background
            g.setColor(avatarConfig.getBackColor());
            g.fillRect(0, 0, avatarLength, avatarLength);
        }

        // draw cells according to data
        g.setColor(avatarConfig.getForeColor());
        for (int i = 0; i < cellCount; i++) {
            for (int j = 0; j < cellCount; j++) {
                if (data[i][j] == 0) continue;
                g.fillRect(padding + i * cellSize, padding + j * cellSize, cellSize, cellSize);
            }
        }

        g.dispose();
        return avatarImage;
    }

}